package cate.game.server.cli.mock;

import cate.game.client.msg.MsgOutput;
import cate.game.server.cli.MsgEncode;
import easy.socket.client.ClientProperties;
import easy.socket.client.EasyClient;
import easy.socket.initial.WsClientInitializer;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class MockNetty extends EasyClient {

    private MockCli parent;

    private ChannelHandlerContext ctx;
    public void setCtx(ChannelHandlerContext value) {
        this.ctx = value;
    }

    public MockNetty(MockCli parent) {
        this.parent = parent;
    }

    public void sendNow(MsgOutput msg) {
        ctx.writeAndFlush(MsgEncode.encode(msg));
    }

    public void play() throws Exception {
        properties = new ClientProperties();
        properties.host = "localhost";// 1.117.20.50 localhost
        properties.ssl = false;
        properties.port = 9900;// 9900 9901
        properties.wsPath = "game";
        WsClientInitializer initializer = new WsClientInitializer();
        initializer.properties(properties);
        initializer.businessHandlerBuilder((ch) -> new MockCliHandler(properties, this.parent));
        this.initializer(initializer);
        this.start((c, ch) -> {
            try {
                for(int i = 0; i < 100; i++) {
                    String times = "【"+i+"】";
                    //TextWebSocketFrame textFrame = new TextWebSocketFrame("I am mock-cli" + times);
                    //ch.writeAndFlush(textFrame);

//                    ByteBuf buf = BBT.create();
//                    buf.writeInt(1998);
//                    String str = "我是" + clientName + times;
//                    byte[] strBytes = str.getBytes(Charset.defaultCharset());
//                    buf.writeInt(strBytes.length);
//                    buf.writeBytes(strBytes);
//                    DemoManyMacProto.Mac mac = NettyHandy.getDemoManyMacProto("iMac Pro"+times, 19999);
//                    byte[] protoBytes = mac.toByteArray();
//                    buf.writeInt(protoBytes.length);
//                    buf.writeBytes(protoBytes);
//                    BinaryWebSocketFrame binFrame = new BinaryWebSocketFrame(buf);
//                    ch.writeAndFlush(binFrame);
                    Thread.sleep(3000);
                }

                //handleConsoleInput(ch);
            } catch (Exception e) {
                log.error("", e);
            }
        });
    }
}
